package com.xidian.zhiancloud.service.ledgerManager.targerAndSystem;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.SecuritySystemLedgerMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.SecuritySystemLedger;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.qo.ledgerManagerment.targetAndSystem.SecuritySystemLedgerQO;
import com.xidian.zhiancloud.util.zyf.StringUtils;
import com.xidian.zhiancloud.vo.ledgerManagerment.targetAndSystem.SecuritySystemLedgerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Summerless
 * @Date: 2020/11/8 23:14
 */
@Service
public class SecuritySystemLedgerService extends ServiceImpl<SecuritySystemLedgerMapper, SecuritySystemLedger> implements IService<SecuritySystemLedger> {
    @Autowired
    private SecuritySystemLedgerMapper securitySystemLedgerMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    public IPage<SecuritySystemLedgerVO> selectByQO(SecuritySystemLedgerQO securitySystemLedgerQO) {
        Page<SecuritySystemLedger> page = new Page<>(securitySystemLedgerQO.getCurrent(), securitySystemLedgerQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);
        LambdaQueryWrapper<SecuritySystemLedger> lambdaQuery = Wrappers.<SecuritySystemLedger>lambdaQuery();
        lambdaQuery.eq(securitySystemLedgerQO.getType() != null, SecuritySystemLedger::getType, securitySystemLedgerQO.getType())
                .eq(securitySystemLedgerQO.getOrganizationId() != null, SecuritySystemLedger::getOrganizationId, securitySystemLedgerQO.getOrganizationId())
                .eq(securitySystemLedgerQO.getStatus() != null, SecuritySystemLedger::getStatus, securitySystemLedgerQO.getStatus())
                .between(securitySystemLedgerQO.getStartTime() != null && securitySystemLedgerQO.getEndTime() != null, SecuritySystemLedger::getCreateTime, securitySystemLedgerQO.getStartTime(), securitySystemLedgerQO.getEndTime())
                .eq(securitySystemLedgerQO.getCompanyId() != null, SecuritySystemLedger::getCompanyId, securitySystemLedgerQO.getCompanyId());
        IPage<SecuritySystemLedger> ans = securitySystemLedgerMapper.selectPage(page, lambdaQuery);
        return ans.convert(u -> {
            SecuritySystemLedgerVO v = new SecuritySystemLedgerVO();
            BeanUtils.copyProperties(u, v);
            Organization organization = organizationMapper.selectById(u.getOrganizationId());
            v.setOrganizationName(organization == null ? "该部门已被删除" : organization.getName());
            if(v.getApplicableOrganizationIds() != null){
                String[] split = u.getApplicableOrganizationIds().split(",");
                List<String> names = Arrays.stream(split).map(id -> {
                    Organization tmp = organizationMapper.selectById(id);
                    return tmp == null ? "该部门已被删除" : tmp.getName();
                }).collect(Collectors.toList());
                v.setApplicableOrganizationNames(StringUtils.concatStrings(names));
            }
            return v;
        });
    }

    public SecuritySystemLedgerVO getSecuritySystemLedgerVOById(Long id) {
        SecuritySystemLedgerVO vo = new SecuritySystemLedgerVO();
        SecuritySystemLedger ledger = securitySystemLedgerMapper.selectById(id);
        if (ledger == null) {
            throw new CustomizeException(GeneralResponseEnums.GET_FAILED);
        }
        BeanUtils.copyProperties(ledger, vo);
        Organization organization = organizationMapper.selectById(ledger.getOrganizationId());
        vo.setOrganizationName(organization == null ? "该部门已被删除" : organization.getName());
        String[] ids_s = ledger.getApplicableOrganizationIds().split(",");
        List<String> names = Arrays.stream(ids_s).map(id_s -> {
            Organization tmp = organizationMapper.selectById(id_s);
            return tmp != null ? tmp.getName() : "该部门已被删除";
        }).collect(Collectors.toList());
        vo.setApplicableOrganizationNames(StringUtils.concatStrings(names));
        return vo;
    }
}
